package com.share.tx.service;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.share.tx.dao.AccountBMapper;
import com.share.tx.entity.AccountB;

@Service
public class AccountBService {
	
	@Autowired
	AccountBMapper accountBMapper;
	
	public void transfer(String outer, String inner, int money){
		HashMap<String, Object> outerHashMap = new HashMap<>();
		HashMap<String, Object> innerHashMap = new HashMap<>();
		
		outerHashMap.put("user", outer);
		outerHashMap.put("money", -money);
		innerHashMap.put("user", inner);
		innerHashMap.put("money", +money);
		
		accountBMapper.updateMoneyByHashMap(outerHashMap);
//		int i = 1/0;
//		try {
//			Thread.sleep(1000000);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		accountBMapper.updateMoneyByHashMap(innerHashMap);
		
	}
	
	public void updateMoneyByAccountB(AccountB accountB){
		accountBMapper.updateMoneyByAccountB(accountB);
		
		//30s后提交事务
		System.out.println("等待中");
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void insertAccountB(AccountB accountB){
		accountBMapper.insertAccountB(accountB);
//		int a = 1/0;
	}
	
//	public void compoundOpt(AccountB accountB1, AccountB accountB2){
//		insertAccountB(accountB1);
//		updateMoneyByAccountB(accountB2);
////		int i = 1/0;
//	}
	
	public void updateMoneyByAccountB1(AccountB accountB){
		accountBMapper.updateMoneyByAccountB(accountB);
	}
	
	public AccountB selectAccountB(AccountB accountB){
		
		return accountBMapper.selectAccountB(accountB);
	}
}
